package com.kkd.kkdapp.adapter;

import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.kkd.kkdapp.R;
import com.kkd.kkdapp.http.entity.MyDiscountCouponDataList;
import com.kkd.kkdapp.utils.LogUtil;
import com.kkd.kkdapp.utils.Tools;

public class MyDiscountCouponAdapter extends BaseAdapter {

	public static final int NOT_UESE = -1;
	List<MyDiscountCouponDataList> mList;
	private boolean mflag;
	private Context mContext;
	public int mPosition;
	private String mCouponId;

	public MyDiscountCouponAdapter(Context context, boolean flag,
			List<MyDiscountCouponDataList> mDataList, int mPosition,
			String mCouponId) {
		super();
		mContext = context;
		mList = mDataList;
		mflag = flag;
		this.mPosition = mPosition;
		this.mCouponId = mCouponId;
	}

	class ViewHolder {
		TextView unit; // 单位
		TextView nominal; // 金额
		TextView typeDesc; // 优惠券类型
		TextView expire_tv; // 过期时间
		ImageView pitch_img;
		ImageView pitch_img1;
		TextView restricted_scene_tv;
		ImageView unit_img;
		TextView investment_tv;
		RelativeLayout useandbeoverdue_bg;
		ImageView state_icon;
	}

	OnTenderItemtListener mOnTenderItemtListener;

	public void setOnTenderItemtListener(OnTenderItemtListener callback) {
		mOnTenderItemtListener = callback;
	}

	public interface OnTenderItemtListener {
		public void onPosition(int id);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mList.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder mViewHolder;
		if (convertView == null) {
			convertView = View.inflate(mContext,
					R.layout.mydiscountcoupon_item, null);
			mViewHolder = new ViewHolder();
			mViewHolder.unit = (TextView) convertView.findViewById(R.id.unit);
			mViewHolder.nominal = (TextView) convertView
					.findViewById(R.id.nominal);
			mViewHolder.typeDesc = (TextView) convertView
					.findViewById(R.id.typeDesc);
			mViewHolder.expire_tv = (TextView) convertView
					.findViewById(R.id.expire_tv);
			mViewHolder.pitch_img = (ImageView) convertView
					.findViewById(R.id.pitch_img);
			mViewHolder.pitch_img1 = (ImageView) convertView
					.findViewById(R.id.pitch_img1);
			mViewHolder.unit_img = (ImageView) convertView
					.findViewById(R.id.unit_img);
			mViewHolder.restricted_scene_tv = (TextView) convertView
					.findViewById(R.id.restricted_scene_tv);
			mViewHolder.investment_tv = (TextView) convertView
					.findViewById(R.id.investment_tv);
			mViewHolder.useandbeoverdue_bg = (RelativeLayout) convertView
					.findViewById(R.id.useandbeoverdue_bg);
			mViewHolder.state_icon = (ImageView) convertView
					.findViewById(R.id.state_icon);
			convertView.setTag(mViewHolder);
		} else {
			mViewHolder = (ViewHolder) convertView.getTag();
		}
		final ViewHolder viewHolder = mViewHolder;
		mViewHolder.nominal.setTag(position);
		mViewHolder.typeDesc.setTag(position);
		mViewHolder.expire_tv.setTag(position);
		mViewHolder.pitch_img1.setTag(position);
		LogUtil.e("ypeColor", mList.get(position).getTypeColor());
		if ("未使用".equals(mList.get(position).getStatus())) {
			mViewHolder.useandbeoverdue_bg.setVisibility(View.GONE);
			mViewHolder.state_icon.setVisibility(View.GONE);
		} else if ("已使用".equals(mList.get(position).getStatus())) {
			mViewHolder.useandbeoverdue_bg.setVisibility(View.VISIBLE);
			mViewHolder.state_icon.setVisibility(View.VISIBLE);
			mViewHolder.state_icon
					.setImageResource(R.drawable.has_been_used_img);
		} else if ("已过期".equals(mList.get(position).getStatus())) {
			mViewHolder.useandbeoverdue_bg.setVisibility(View.VISIBLE);
			mViewHolder.state_icon.setVisibility(View.VISIBLE);
			mViewHolder.state_icon.setImageResource(R.drawable.past_due_img);
		}
		// 类型
		mViewHolder.typeDesc.setText(mList.get(position).getTypeDesc());
		if (mList.get(position).getType() == 4) {
			// 金额
			mViewHolder.nominal.setText(Tools.intOrDouble(mList.get(position)
					.getAmount() * 0.01));
			mViewHolder.unit_img.setVisibility(View.VISIBLE);
			mViewHolder.unit.setVisibility(View.GONE);
			mViewHolder.nominal.setRight(R.id.unit_img);
			mViewHolder.restricted_scene_tv.setText(mList.get(position)
					.getUseProduct());
			mViewHolder.investment_tv.setText(mList.get(position).getSource());
		} else if (mList.get(position).getType() == 2) {
			mViewHolder.nominal.setText(Tools.intOrDouble(mList.get(position)
					.getAmount() * 0.01) + "%");
			mViewHolder.unit.setText("+");
			mViewHolder.unit.setVisibility(View.VISIBLE);
			mViewHolder.unit_img.setVisibility(View.GONE);
			mViewHolder.investment_tv.setText(mList.get(position).getSource()
					+ ",起投金额" + mList.get(position).getLowestAmount() / 100
					+ "元");
			mViewHolder.restricted_scene_tv.setText(mList.get(position)
					.getUseProduct());
		} else {
			// 金额
			mViewHolder.nominal.setText(Tools.intOrDouble(mList.get(position)
					.getAmount() * 0.01));
			mViewHolder.unit_img.setVisibility(View.VISIBLE);
			mViewHolder.unit.setVisibility(View.GONE);
			mViewHolder.restricted_scene_tv.setText(mList.get(position)
					.getUseProduct());
			mViewHolder.investment_tv.setText(mList.get(position).getSource()
					+ ",起投金额" + mList.get(position).getLowestAmount() / 100
					+ "元");
		}
		if (mflag) {
			if (mCouponId.equals(mList.get(position).getCouponId())) {
				viewHolder.pitch_img.setVisibility(View.VISIBLE);
			} else {
				viewHolder.pitch_img.setVisibility(View.GONE);
			}
		}
		// 过期时间
		mViewHolder.expire_tv.setText("有效期 "
				+ mList.get(position).getBeginDate()

				+ " 至 " + mList.get(position).getEndDate());
		convertView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (mflag) {
					mPosition = NOT_UESE;
					mCouponId = "";
					viewHolder.pitch_img1.setVisibility(View.VISIBLE);
					notifyDataSetChanged();
				}
				if (mOnTenderItemtListener != null) {
					mOnTenderItemtListener.onPosition(position);
				}
			}
		});
		return convertView;
	}
}
